/**
* @depends jquery.ui.js
*/

(function($) {

	$.widget( 'ui.widget_flow_genericflow_item', {
		
		_init: function() {
			
			var item = this.element;
			var content = $('> .Content', this.element);
			var item_id = item.attr( 'item_id' );
			var like = $('> ul > .Like', this.element);
			
			content.find( 'p' ).morify();
			
			$('a[action=comment]', content).click( function() {
				$(this).closest( 'li' ).find( '.Writer' ).addClass( 'Visible' ).find( 'textarea' ).focus();
			} );
			
			like.bind( 'like-refresh', function() {
				if( $('p', this).html() ) {
					$(this).addClass( 'Visible' );
				}
				else {
					$(this).removeClass( 'Visible' );
				}
			} );

			var likeAction = $('[action=like]', content);
			likeAction.toggler( {
				messages: [ 'Like', 'Unlike' ],
				currentIndex : parseInt(likeAction.attr( 'currentIndex' )),
				async: function( index ) {
					$.wajax( 'Flow_GenericFlow_Item_' + (index ? 'Like' : 'Unlike'), {
						id : item_id
					}, function( json ) {
						if( json.status != 'success' ) {
							return false;
						}
						
						$('p', like).html( json.content );
						like.triggerHandler( 'like-refresh' );
						likeAction.toggler( 'toggle' );
					} );
				}
				
			} );
			
			this._initWriter();
			this._initExpander();
			this._initDelete();
			
			$('.Widget_Flow_GenericFlow_Comment', item).widget_flow_genericflow_comment();
			
		},
		
		_initExpander: function() {
		
			var $this = this;
			var item = this.element;
			var item_id = item.attr( 'item_id' );
			var o = $('.View a', item);
			o.click( function() {
				$.wajax( 'Flow_GenericFlow_Item_Comments', {
					id: item_id
				}, function( json ) {
					if( json.status != 'success' ) {
						return false;
					}
					var data = $(json.content);
					item.find( 'ul li:not(.Writer):not(.Like)' ).replaceWith( data );
					data.find( '.Comment' ).widget_flow_genericflow_comment();
					MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
				} );
			} );
		},
		
		_initWriter: function() {
		
			var $this = this;
			
			var o = $('.Writer', this.element);
			var button = $('.Button', this.element);
			var textarea = $('textarea', this.element);
			
			textarea
				.textarea( {
					defaultText : 'Write a comment...'
				} )
				.focus( function() {
					o.addClass( 'Focus' );
				} )
				.blur( function() {
					$this.refresh();
				} );

			button
				.mousedown( function( e ) {
					e.preventDefault();
				} )
				.click( function() {

					var item_id = $this.element.attr( 'item_id' );
					var message = textarea.attr( 'value' );
					$.wajax( 'Flow_GenericFlow_Comment_Add', {
						id : item_id,
						message : message
					}, function( json ) {
						if( json.status !== 'success' ) {
							return false;
						}
						var li = $(json.content);
						li.hide();
						li.insertBefore(o);
						li.slideDown( 'fast' );
						li.find( '.Widget_Flow_GenericFlow_Comment' ).widget_flow_genericflow_comment();
						textarea.attr( 'value', '' ).blur();
						MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
					} );
				} );
		},
		
		_initDelete: function() {
		
			var $this = this;
			var item_id = this.element.attr( 'item_id' );
			var del = $('> .Content > .Delete.Icon', this.element);
			
			del.tooltip( {
				tip: 'Delete post'
			} );
			
			del.click( function() {
			
				var dialog = $.window.create()
					.dialog( {
						title: 'Confirmation',
						message: 'Are you sure you want to delete this post ?',
						icon: $.dialog.icons.warning,
						buttons: {
							yes: function() {
								dialog.dialog( 'close' );
								$.wajax( 'Flow_GenericFlow_Item_Delete', {
									id: item_id
								}, function( json ) {
									if( json.status != 'success' ) {
										return false;
									}
									$this.element.slideUp( 'fast', function() {
										$this.element.parent().remove();
									} );
								} );
							},
							cancel: function() {
								dialog.dialog( 'close' );
							}
						}
					} )
					.dialog( 'open' );
			} );
		},
		
		refresh: function() {
		
			var $this = this;
			
			var o = $('.Writer', this.element);
			var textarea = $('textarea', this.element);

			if( textarea.hasClass( 'ui-default' ) ) {
				if( this.element.find( '.Widget_Flow_GenericFlow_Comment' ).length == 0 ) {
					o.removeClass( 'Visible' );
				}
				o.removeClass( 'Focus' );
			}
		}

	} );

})(jQuery);
